package com.wangijun.yuan.web.api;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.toolkit.Db;
import com.wangijun.yuan.web.base.CommonPage;
import com.wangijun.yuan.web.base.CommonResult;
import com.wangijun.yuan.core.domain.Comment;
import com.wangijun.yuan.core.domain.ReportInfo;
import com.wangijun.yuan.core.model.IdParam;
import com.wangijun.yuan.core.model.em.ReportTypeEnum;
import com.wangijun.yuan.web.model.dto.CommentPageParam;
import com.wangijun.yuan.web.model.dto.CommentSaveParam;
import com.wangijun.yuan.web.model.dto.ReplyPageParam;
import com.wangijun.yuan.web.model.vo.CommentVO;
import com.wangijun.yuan.web.model.vo.ReplyCmtVO;
import com.wangijun.yuan.web.service.CommentService;
import com.wangijun.yuan.web.service.OperateInfoService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Tag(name = "6-评论接口")
@RestController
@RequiredArgsConstructor
@RequestMapping("/comment")
public class CommentController {

    private final CommentService commentService;
    private final OperateInfoService operateInfoService;

    @Operation(summary = "分页查询评论列表")
    @PostMapping("/list")
    public CommonResult<CommonPage<CommentVO>> list(@RequestBody CommentPageParam param) {
        List<Long> commentIds = operateInfoService.getCommentIds();
        return CommonResult.data(commentService.list(param, commentIds));
    }

    @Operation(summary = "回复分页查询")
    @PostMapping("/reply")
    public CommonResult<CommonPage<ReplyCmtVO>> reply(@RequestBody ReplyPageParam param) {
        List<Long> commentIds = operateInfoService.getCommentIds();
        return CommonResult.data(commentService.reply(param, commentIds));
    }


    @Operation(summary = "删除评论",description = "返回删除评论数量")
    @PostMapping("/del")
    public CommonResult<Integer> del(@RequestBody IdParam param) {
        return CommonResult.data(commentService.del(param.getId()));
    }

    public record CommentIdParam(
            @Schema(description = "评论ID")
            Long commentId) {
    }

    @Operation(summary = "评论点赞，或取消点赞")
    @PostMapping("/liked")
    public CommonResult<Boolean> liked(@RequestBody CommentIdParam param) {
        return CommonResult.data(commentService.liked(param.commentId));
    }

    @Operation(summary = "添加评论", description = "添加评论")
    @PostMapping("/add")
    public CommonResult<CommentVO> add(@RequestBody CommentSaveParam param) {
        return CommonResult.data(commentService.add(param));
    }


    public record ReportParam(
            @Schema(description = "评论ID")
            Long commentId,
            @Schema(description = "举报原因")
            String remark) {
    }

    @Operation(summary = "举报评论", description = "举报评论")
    @PostMapping("/report")
    public CommonResult<Boolean> report(@RequestBody ReportParam param) {
        if (param.commentId == null) {
            CommonResult.error("举报评论ID不可为空");
        }
        Comment comment = commentService.getById(param.commentId);
        if (comment == null) {
            CommonResult.error("举报评论不存在");
        }
        if (StrUtil.isBlank(param.remark)) {
            CommonResult.error("举报原因不可为空");
        }
        Db.save(ReportInfo.builder()
                .reportDataId(param.commentId)
                .reason(param.remark)
                .comment(comment.getContent())
                .reportType(ReportTypeEnum.comment)
                .build());
        return CommonResult.ok();
    }
}
